/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VeterinarioSelector.java
 *
 * Created on Apr 18, 2009, 7:54:44 AM
 */

package doghost.selectors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.VeterinarioController;
import doghost.entities.Veterinario;

/**
 *
 * @author duo
 */
public class VeterinarioSelector extends javax.swing.JDialog {

    protected VeterinarioController veterinarioController;
    protected Veterinario selectedVeterinario;

    /** Creates new form VeterinarioSelector */
    public VeterinarioSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        veterinarioController = new VeterinarioController();
        veterinarioTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("veterinarioRecordSelected",
                                !isVeterinarioRecordSelected(), isVeterinarioRecordSelected());
                    }
                });
    }

    public Veterinario getSelectedVeterinario() {
        return selectedVeterinario;
    }

    public void setSelectedVeterinario(Veterinario selectedVeterinario) {
        this.selectedVeterinario = selectedVeterinario;
    }

    public boolean isVeterinarioRecordSelected() {
        return veterinarioTable.getSelectedRow() != -1;
    }

    @Action
    public void newVeterinario() {
        veterinarioController.newVeterinario(doghostPUEntityManager, veterinarioList);
    }

    @Action(enabledProperty = "veterinarioRecordSelected")
    public void editVeterinario() {
        int index = veterinarioTable.getSelectedRow();
        veterinarioController.setVeterinario(veterinarioList.get(veterinarioTable.
                convertRowIndexToModel(index)));
        veterinarioController.editVeterinario(doghostPUEntityManager, veterinarioList,
                veterinarioTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "veterinarioRecordSelected")
    public void deleteVeterinario() {
        int index = veterinarioTable.getSelectedRow();
        veterinarioController.setVeterinario(veterinarioList.get(veterinarioTable.
            convertRowIndexToModel(index)));
        veterinarioController.deleteVeterinario(doghostPUEntityManager, veterinarioList,
            veterinarioTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelVeterinarioSelector() {
        setSelectedVeterinario(null);
        setVisible(false);
    }

    @Action(enabledProperty = "veterinarioRecordSelected")
    public void selectVeterinario() {
        int index = veterinarioTable.getSelectedRow();
        setSelectedVeterinario(veterinarioList.get(veterinarioTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterVeterinario() {
        TableRowSorter veterinarioSorter = (TableRowSorter) veterinarioTable.getRowSorter();
        String text = veterinarioFilterTextField.getText();
        veterinarioFilterTextField.setText("");
        if(text.length()==0) {
            veterinarioSorter.setRowFilter(null);
        } else {
            veterinarioSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(VeterinarioSelector.class);
        veterinarioQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery(resourceMap.getString("veterinarioQuery.query")); // NOI18N
        veterinarioList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(veterinarioQuery.getResultList());
        veterinarioFilterTextField = new javax.swing.JTextField();
        veterinarioFilterButton = new javax.swing.JButton();
        veterinarioSelectionPanel = new org.jdesktop.swingx.JXPanel();
        veterinarioScrollPane = new javax.swing.JScrollPane();
        veterinarioTable = new javax.swing.JTable();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        selectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        veterinarioFilterTextField.setText(resourceMap.getString("veterinarioFilterTextField.text")); // NOI18N
        veterinarioFilterTextField.setName("veterinarioFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(VeterinarioSelector.class, this);
        veterinarioFilterButton.setAction(actionMap.get("filterVeterinario")); // NOI18N
        veterinarioFilterButton.setText(resourceMap.getString("veterinarioFilterButton.text")); // NOI18N
        veterinarioFilterButton.setName("veterinarioFilterButton"); // NOI18N

        veterinarioSelectionPanel.setName("veterinarioSelectionPanel"); // NOI18N

        veterinarioScrollPane.setName("veterinarioScrollPane"); // NOI18N

        veterinarioTable.setAutoCreateRowSorter(true);
        veterinarioTable.setName("veterinarioTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, veterinarioList, veterinarioTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${telefone}"));
        columnBinding.setColumnName("Telefone");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mobile}"));
        columnBinding.setColumnName("Mobile");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        veterinarioScrollPane.setViewportView(veterinarioTable);
        veterinarioTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("veterinarioTable.columnModel.title0")); // NOI18N
        veterinarioTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("veterinarioTable.columnModel.title1")); // NOI18N
        veterinarioTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("veterinarioTable.columnModel.title2")); // NOI18N

        javax.swing.GroupLayout veterinarioSelectionPanelLayout = new javax.swing.GroupLayout(veterinarioSelectionPanel);
        veterinarioSelectionPanel.setLayout(veterinarioSelectionPanelLayout);
        veterinarioSelectionPanelLayout.setHorizontalGroup(
            veterinarioSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 616, Short.MAX_VALUE)
            .addGroup(veterinarioSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(veterinarioSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(veterinarioScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 592, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        veterinarioSelectionPanelLayout.setVerticalGroup(
            veterinarioSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 292, Short.MAX_VALUE)
            .addGroup(veterinarioSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(veterinarioSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(veterinarioScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        newButton.setAction(actionMap.get("newVeterinario")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editVeterinario")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteVeterinario")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelVeterinarioSelector")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        selectButton.setAction(actionMap.get("selectVeterinario")); // NOI18N
        selectButton.setName("selectButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(veterinarioFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(veterinarioFilterButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(newButton)
                        .addGap(18, 18, 18)
                        .addComponent(editButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton)
                        .addGap(37, 37, 37)
                        .addComponent(selectButton)))
                .addContainerGap(233, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(veterinarioSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(selectButton)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(veterinarioFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(veterinarioFilterButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 342, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(editButton)
                            .addComponent(newButton)
                            .addComponent(deleteButton)
                            .addComponent(cancelButton))))
                .addGap(26, 26, 26))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(70, 70, 70)
                    .addComponent(veterinarioSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(82, Short.MAX_VALUE)))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VeterinarioSelector dialog = new VeterinarioSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton selectButton;
    private javax.swing.JButton veterinarioFilterButton;
    private javax.swing.JTextField veterinarioFilterTextField;
    private java.util.List<doghost.entities.Veterinario> veterinarioList;
    private javax.persistence.Query veterinarioQuery;
    private javax.swing.JScrollPane veterinarioScrollPane;
    private org.jdesktop.swingx.JXPanel veterinarioSelectionPanel;
    private javax.swing.JTable veterinarioTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
